package com.kantboot.project.auth.user.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.kantboot.project.wechat.applet.config.WechatAppletConfig;
import com.kantboot.project.wechat.pay.config.WechatPayConfig;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;


@Entity
@Table(name="ces_auth_wechat_applet")
@Getter
@Setter
@DynamicInsert(true)
@DynamicUpdate(true)
@Accessors(chain = true)
@EqualsAndHashCode
public class CesAuthWechatApplet implements Serializable {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    /**
     * 标题
     */
    @Column(name="title")
    private String title;

    @Column(name="app_id")
    private String appId;

    @Column(name="app_secret")
    private String appSecret;

    /**
     * 描述
     */
    @Column(name="content")
    private String content;

    /**
     * 微信支付id
     */
    @Column(name="pay_wechat_pay_id")
    private Long payWechatPayId;


    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name="gmt_create",columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date gmtCreate;

    /**
     * 最后一次修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name="gmt_modified",columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
    private Date gmtModified;

    @OneToOne(targetEntity = CesAuthPayWechatPay.class,
            fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "pay_wechat_pay_id",referencedColumnName = "id",insertable = false,updatable = false)
    private CesAuthPayWechatPay payWechatPay;

    public WechatAppletConfig getWechatAppletConfig(){
        if(payWechatPayId==null){
            return null;
        }
        return new WechatAppletConfig().setSecret(appSecret).setAppid(appId);
    }

    public WechatPayConfig getWechatPayConfig(){
        if(payWechatPayId==null){
            return null;
        }
        return new WechatPayConfig()
                .setAppid(appId)
                .setSecret(appSecret)
                .setNotifyUri(payWechatPay.getNotifyUri())
                .setMchKey(payWechatPay.getMchKey())
                .setMchId(payWechatPay.getMchId());
    }

}
